const Goods = require("../model/goods.model")
class GoodsService {
  /**创建商品 */
  async createGoods (goods) {
    const res = await Goods.create(goods)
    return res.dataValues
  }
  /**修改商品信息 */
  async updateGoodsInfoById (id, goods) {
    const res = await Goods.update(goods, { where: { id } })
    return res[0] > 0 ? true : false
  }
  /**下架商品信息,软删除操作 */
  async removeGoodsInfoByID (id) {
    const res = await Goods.destroy({ where: { id } })
    return res > 0 ? true : false
  }
  /**删除商品信息,硬删除操作 */
  async restoreGoodsInfoByID (id) {
    const res = await Goods.restore({ where: { id } })
    return res > 0 ? true : false
  }
  /**获取商品列表 */
  async getGoodsList (pageNum, pageSize) {
    // 一、分别查询
    // const count = await Goods.count()
    // const offset = (pageNum - 1) * pageSize
    // const rows = await Goods.findAll({ offset, limit: pageSize * 1 })
    // 二、整合查询
    const offset = (pageNum - 1) * pageSize
    const { count, rows } = await Goods.findAndCountAll({ offset, limit: pageSize * 1 })
    return {
      pageNum,
      pageSize,
      total: count,
      rows
    }
  }
}
module.exports = new GoodsService()